package com.lagou.edu.service.impl;

import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 转账service
 */
@Service(value = "transferService")
@Repository
public class TransferServiceImpl implements TransferService {

    @Autowired
    @Qualifier(value = "jdbcTemplateDaoImpl")
    private AccountDao accountDao;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);
        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);
        try {
            accountDao.updateAccountByCardNo(to);
            int c = 1/0;    // 制造异常
            accountDao.updateAccountByCardNo(from);
        } catch (Exception e) {
            throw new Exception("失败:" + e.getMessage());
        }
    }
}
